# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgname=soju
pkgver=0.6.2
pkgrel=4
pkgdesc='A user-friendly IRC bouncer'
arch=('loong64' 'x86_64')
url='https://soju.im'
license=('AGPL3')
depends=('glibc')
makedepends=(
  'git'
  'go'
  'scdoc'
  'sqlite'
)
optdepends=(
  'postgresql: PostgreSQL support'
  'sqlite: SQLite support'
)
backup=('etc/soju/config')
options=('!lto')
_commit='fa94179363613538a4e56a5ef5bc90d070763e8d'
source=(
  "$pkgname::git+https://git.sr.ht/~emersion/soju#commit=$_commit"
  'systemd.service.patch'
  'sysusers.conf'
  'tmpfiles.conf'
)
sha512sums=('SKIP'
            'f7703a25aece3c3ef6faa6be31bb80ce83c839a7e3615f155cea30b66c88bc0fcf96d7387757f6b007d4514ba6ef7b91119182eb48b11b7167af7939be9dc241'
            '7b4069043d204581b0eda376956f8996365631e83ca78e002d6ea0caec931605bb5e99cf4f42a75b6de958267b982ee8ed7f31046c6b97d8f2442430678f3c07'
            '378dd64a8c3fa4fd1b28c055bbeb7c46eb05ec79a925d5b138af5d373970e82bafba62ff29269f63e67c67f34fb961570d448a02fa08f7f6cf4e8d844a3c12a0')
b2sums=('SKIP'
        '800d7febb692d53de54080622795467a82730aa1a116740d8bc2b8577a0afce2c4c45904defc5d1de6e98cd77f3f5eb1a7f7972ce21215251d4b7dce8f4fdcc9'
        '14e951b98e80d0dddf08aa0c2b03c57a86bda5ea1fd62d332e56edaf42f478ff86e9367699ab83b1ce0b968068e31048573b703bca6f4900cea901ad518a94b7'
        '9d164e694980e40955b1e8cedf1875214299840a8fce09b20069d594e74265c4335c0693e4bdd7c073c062a1de66cca859657f7581a27baffbfab741f45d0acc')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download

  # patch systemd service
  patch -p1 -i "$srcdir/systemd.service.patch"
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS} \
    -X git.sr.ht/~emersion/soju/config.DefaultPath=/etc/soju/config \
    -X git.sr.ht/~emersion/soju/config.DefaultUnixAdminPath=/run/soju/admin" \
    -tags 'libsqlite3' \
    -o build \
    ./cmd/... \
    ./contrib/...

  # rename binaries to prevent file conflicts
  mv build/{migrate-db,/soju-migrate-db}
  mv build/{migrate-logs,/soju-migrate-logs}
  mv build/{znc-import,soju-znc-import}

  # man page
  scdoc < doc/soju.1.scd > build/soju.1

  # ensure reproducibility of man page
  # i'm not 100% sure where the man sub-command gets the date from, i assume
  # it uses $TODAYS_DATE, so this should make the man page reproducible
  local _commit_date=$(git show --no-patch --format=%cd --date=format:%Y-%m-%d)
  sed \
    -i build/soju.1 \
    -e "s/\"[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\"/\"$_commit_date\"/"
}

check() {
  cd "$pkgname"

  go test -v ./...
}

package() {
  cd "$pkgname"

  # systemd integration
  install -vDm644 -t "$pkgdir/usr/lib/systemd/system" contrib/soju.service
  install -vDm644 "$srcdir/sysusers.conf" "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm644 "$srcdir/tmpfiles.conf" "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  # binaries
  install -vDm755 -t "$pkgdir/usr/bin" build/soju{,db,ctl,-migrate-db,-migrate-logs,-znc-import}
  install -vDm755 contrib/casemap-logs.sh "$pkgdir/usr/bin/soju-casemap-logs"

  # man page
  install -vDm644 -t "$pkgdir/usr/share/man/man1" build/soju.1

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md contrib/clients.md
  cp -vr doc "$pkgdir/usr/share/doc/$pkgname"
  rm -f "$pkgdir/usr/share/doc/$pkgname/doc/soju.1.scd"

  # configuration
  install -vDm644 config.in "$pkgdir/etc/soju/config"
}
